package com.aaa.dao.impl;

import com.aaa.dao.CityDao;
import com.aaa.entity.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class CityDaoImpl implements CityDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    private RowMapper<City> rowMapper = new RowMapper<City>() {
        public City mapRow(ResultSet resultSet, int i) throws SQLException {
            City city = new City();
            city.setTid(resultSet.getLong("tid"));
            city.setTname(resultSet.getString("tname"));
            return city;
        }
    };

    public List<City> listAll(){
        String sql = "select tid,tname from city";
        List<City> result = this.jdbcTemplate.query(sql, rowMapper);
        return result;
    }
}
